﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ESRI.ArcGIS.Geodatabase;
using ESRI.ArcGIS.Geometry;

namespace CheckData
{
    /// <summary>
    /// 检查数据是否重复 并且修复
    /// </summary>
    public class CheckSame
    {
        /// <summary>
        /// 检查数据 采用重心法
        /// </summary>
        /// <param name="fclass"></param>
        /// <param name="checkFields">要检查相同的字段 空则不检查</param>
        public void Check1(IFeatureClass fclass, string[] checkFields)
        {
            // 扫描数据
            int num = 0;
            Dictionary<string, List<IFeature>> dic = new Dictionary<string, List<IFeature>>();    // 字典
            IFeatureCursor cursor = fclass.Search(null, false);
            for (IFeature feature = cursor.NextFeature(); feature != null; feature = cursor.NextFeature()) {
                num++;
                if (num % 1000 == 0) System.Windows.Forms.Application.DoEvents();
                if (feature.Shape.IsEmpty) continue;
                
                double[] nums = GetCentroid(feature.Shape);
                string key = string.Format("{0:0.0}|{1:0.0}|{2:0.0}", nums[0], nums[1], nums[2]);
                foreach (string checkField in checkFields) {
                    int index = feature.Fields.FindField(checkField);
                    if (index < 0) continue;
                    string value = feature.get_Value(index).ToString().Trim();
                    key += "|" + value;
                }
                List<IFeature> oids = new List<IFeature>();
                if (dic.TryGetValue(key, out oids)) oids.Add(feature);
                else { dic[key] = new List<IFeature>() { feature }; }
            }

            // 删除重复
            num = 0;
            foreach (KeyValuePair<string, List<IFeature>> item in dic) {
                num++;
                if (num % 1000 == 0) System.Windows.Forms.Application.DoEvents();
                if (item.Value.Count < 2) continue;

                for (int i = 1; i < item.Value.Count; i++) {
                    item.Value[i].Delete();
                }
            }

        }
        /// <summary>
        /// 检查数据 采用相同节点法
        /// </summary>
        /// <param name="fclass"></param>
        /// <param name="checkFields">要检查相同的字段 空则不检查</param>
        public void Check2(IFeatureClass fclass, string[] checkFields)
        {

        }
        /// <summary>
        /// 获取图像的质心 点 线:中点 面:质心 其他:矩形范围中点
        /// </summary>
        /// <param name="shape"></param>
        /// <returns>返回一个三维数组</returns>
        private double[] GetCentroid(IGeometry shape)
        {
            // 多1个维度加强约束条件
            if (shape.GeometryType == esriGeometryType.esriGeometryPoint){
                IPoint point = shape as IPoint;
                return new double[] { point.X, point.Y, point.Z };
            }
            else if (shape.GeometryType == esriGeometryType.esriGeometryPolyline) {
                IPolyline line = shape as IPolyline;
                IPoint point =  new PointClass();
                line.QueryPoint(esriSegmentExtension.esriNoExtension, line.Length / 2, false, point);
                return new double[] { point.X, point.Y, line.Length };      // 线长度
            }
            else if (shape.GeometryType == esriGeometryType.esriGeometryPolygon) {
                IArea area = shape as IArea;
                return new double[] { area.Centroid.X, area.Centroid.Y, area.Area };    // 面积
            }
            else {
                return new double[] { (shape.Envelope.XMin + shape.Envelope.XMax) / 2.0, (shape.Envelope.YMin + shape.Envelope.YMax) / 2, 0 };
            }
        }

    }
}
